﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MacomberMapSystem.Common.Database;
using MacomberMapSystem.Common.Integration.Blackstart;
using MacomberMapSystem.Common.CIM;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    /// <summary>
    /// This form handles interactions to the blackstart system
    /// </summary>
    public partial class frmBlackstartElement : Form
    {
        #region Variable declarations
        /// <summary>The model associated with the display</summary>
        public MM_Database_Model Model;

        /// <summary>The selected substation</summary>
        public CIM_Element Substation
        {
            get
            {
                return Model.Substations[cmbSubstation.SelectedItem.ToString()];
            }
        }

        /// <summary>Our selected element</summary>
        public CIM_Element SelectedElement
        {
            get
            {
                return cmbElement.SelectedItem as CIM_Element;
            }
        }

        /// <summary>
        /// Return the selected action
        /// </summary>
        public MM_Blackstart_Corridor_Element.enumElementActivity Action
        {
            get
            {
                return (MM_Blackstart_Corridor_Element.enumElementActivity)cmbEvent.SelectedItem;
            }
        }

        #endregion

        #region Initialization
        /// <summary>
        /// Initialize an element find window based on subsatation/elemtype/elem
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="Synchroscope"></param>
        public frmBlackstartElement(MM_Database_Model Model, MM_Synchroscope Synchroscope)
        {
            InitializeComponent();
            this.Model = Model;
            this.StartPosition = FormStartPosition.CenterParent;
            cmbEvent.Visible = lblEvent.Visible = false;
            OneLinePreview.ElementClicked += new MacomberMapSystem.Common.User_Interfaces.One_Lines.ctlOneLinePreview.ElementClickDelegate(OneLinePreview_ElementClicked);
            cmbSubstation.Items.AddRange(Model.Substations.Keys.ToArray());
            if (Synchroscope != null)
            {
                cmbSubstation.Text = Synchroscope.Substation;
                if (Synchroscope.RdfID != null)
                {
                    CIM_Element Elem = Model.FromRdfID(Synchroscope.RdfID);
                    cmbElementType.SelectedItem = Elem.ElemType;
                    cmbElement.SelectedItem = Elem;
                }
            }
        }


        /// <summary>
        /// Initialize a new blackstart element
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="Elem"></param>
        public frmBlackstartElement(MM_Database_Model Model, MM_Blackstart_Corridor_Element Elem, MM_Blackstart_Corridor_Element[] Elements)
        {
            InitializeComponent();
            this.Model = Model;
            this.StartPosition = FormStartPosition.CenterParent;
            
            OneLinePreview.ElementClicked += new MacomberMapSystem.Common.User_Interfaces.One_Lines.ctlOneLinePreview.ElementClickDelegate(OneLinePreview_ElementClicked);
            //Add in our substation information, and our list of event types
            try
            {
                foreach (MM_Blackstart_Corridor_Element.enumElementActivity ElemEvent in Enum.GetValues(typeof(MM_Blackstart_Corridor_Element.enumElementActivity)))
                    cmbEvent.Items.Add(ElemEvent);
                cmbSubstation.Items.AddRange(Model.Substations.Keys.ToArray());
                if (Elem != null)
                {
                    cmbEvent.SelectedItem = Elem.Action;
                    if (Elem.Substation != null)
                        cmbSubstation.Text = Elem.Substation.Name;
                    if (Elem.AssociatedElement != null)
                    {
                        cmbElementType.SelectedItem = Elem.AssociatedElement.ElemType;
                        cmbElement.SelectedItem = Elem.AssociatedElement;
                    }
                }
                OneLinePreview.BlackstartElements = Elements;
            }
            catch 
            {
            }
        }

        /// <summary>
        /// Handle the clicking of an element
        /// </summary>
        /// <param name="xElem"></param>
        private void OneLinePreview_ElementClicked(System.Xml.XmlElement xElem)
        {
            //Update our components to support the found element.
            CIM_Element SelectedElem = Model.FromRdfID(xElem.Attributes["rdfID"].Value);
            cmbElementType.Text = SelectedElem.ElemType;
            cmbElement.SelectedItem = SelectedElem;
        }
        #endregion

        #region UI interactions
        /// <summary>
        /// Handle the expand/contract button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picExpand_Click(object sender, EventArgs e)
        {
            if (this.FormBorderStyle == FormBorderStyle.FixedToolWindow)
            {
                this.splMain.Panel2Collapsed = false;
                this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
                this.Size = new Size(763, 391);
                this.splMain.SplitterDistance = 261;
            }
            else
            {
                this.splMain.Panel2Collapsed = true;
                this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
                this.Size = new Size(261, 200);                
            }
        }

        /// <summary>
        /// Handle the change of substation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSubstation_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbElementType.Items.Clear();
            cmbElement.Items.Clear();
            if (cmbSubstation.SelectedItem != null)
            {
                CIM_Element Sub = Model.Substations[cmbSubstation.SelectedItem.ToString()];
                
                
                if (OneLinePreview.ActiveOneLine == null || !OneLinePreview.ActiveOneLine.rdfID.Equals(Sub.rdfID))
                    using (System.Data.Common.DbCommand dCmd = Model.Repository.Db.CreateCommand("SELECT ID FROM MM_DATABASE_ONELINE WHERE RDFID=:0 AND ONELINETYPE=0 AND MODELS LIKE :1", Sub.rdfID.ToString(), "%," + Model.ID.ToString() + ",%"))
                    using (System.Data.Common.DbDataReader dRd = dCmd.ExecuteReader())
                        if (dRd.Read())
                        {
                            Bitmap OLImg = Model.Repository.Db.LoadOneLineImage(Convert.ToInt32(dRd["ID"]));
                            MM_Database_OneLine OneLine = Model.Repository.Db.LoadOneLine(Convert.ToInt32(dRd["ID"]), Model.Repository);
                            OneLinePreview.AssignOneLine(OneLine, OLImg, Model, Model.Repository);
                        }

                foreach (CIM_Element Elem in Sub.Elements)
                    if (!cmbElementType.Items.Contains(Elem.ElemType))
                        cmbElementType.Items.Add(Elem.ElemType);
            }
        }

        /// <summary>
        /// When the element type is selected, show accordingly
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbElementType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSubstation.SelectedItem != null)
            {
                CIM_Element Sub = Model.Substations[cmbSubstation.SelectedItem.ToString()];
                cmbElement.Items.Clear();
                foreach (CIM_Element Elem in Sub.Elements)
                    if (Elem.ElemType == cmbElementType.Text)
                        cmbElement.Items.Add(Elem);
            }
        }
        #endregion

        private void btnOk_Click(object sender, EventArgs e)
        {

        }

    }
}
